package com.common;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : CodeGenerator  类名
 * @Description :   描述
 */
public class CodeGenerator {
    public static void main(String[] args) throws InterruptedException {
        AutoGenerator mpg = new AutoGenerator();


//        String[] tableNameList = {"bi_overall_sales_indicators","bi_sales_revenue","bi_business_income","bi_world_map_data","bi_annual_data","bi_sku_model_number_data",
        // 需要生成的表(表中需有is_delete字段，默认0为正常,1为删除)
        String[] tableNameList = {"client_order_package_insurance"};
        // 作者
        String author = "eden";

        // 包路径（如需生成admin 更换为 com.yd.mall）
        String parent = "com.yd.mall";
//        String parent = "com.yd.mall.portal";
//        String parent = "com.yd.outer";

        // 文件路径 （如需生成admin 更换为 com/yd/mall）
        String filePath = "com/yd/mall";
//        String filePath = "com/yd/mall/portal";
//        String filePath = "com/yd/outer";

        // 项目文件 (如需生成admin 更换为 /mall-admin)
        String projectDocuments = "/mall-admin";
//        String projectDocuments = "/mall-portal";
//        String projectDocuments = "/mall-outer";

        // 数据库相关信息
        String jdbcUrl = "jdbc:mysql://10.0.1.137:3306/aiper?useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true";
        String jdbcName = "root";
        String jdbcPwd = "FPYz2ZD^gu!*";

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + projectDocuments + "/src/main/java");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(true);
        gc.setOpen(false);
        gc.setSwagger2(true);
        gc.setAuthor(author);

        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        // 这里以mysql为例
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl(jdbcUrl);
        dsc.setUsername(jdbcName);
        dsc.setPassword(jdbcPwd);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(parent)
                .setController("controller")
                .setEntity("domain")
                .setService("service")
                .setServiceImpl("service.impl")
                .setMapper("mapper");

        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + projectDocuments + "/src/main/resources/" + filePath + "/mapper/"
                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        focList.add(new FileOutConfig("/templates/controller.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + projectDocuments + "/src/main/java/" + filePath + "/controller/"
                        + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/entity.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + projectDocuments + "/src/main/java/" + filePath + "/domain/"
                        + tableInfo.getEntityName() + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/vo.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + projectDocuments + "/src/main/java/" + filePath + "/domain/vo/"
                        + tableInfo.getEntityName() + "Vo" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/request.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + projectDocuments + "/src/main/java/" + filePath + "/domain/request/"
                        + tableInfo.getEntityName() + "Request" + StringPool.DOT_JAVA;
            }
        });

        focList.add(new FileOutConfig("/templates/service.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + projectDocuments + "/src/main/java/" + filePath + "/service/"
                        + tableInfo.getEntityName() + "Service" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/serviceImpl.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + projectDocuments + "/src/main/java/" + filePath + "/service/impl/"
                        + tableInfo.getEntityName() + "ServiceImpl" + StringPool.DOT_JAVA;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

//        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
//        templateConfig.setXml("/templates/mapper.xml");
        templateConfig.setXml(null);
        templateConfig.setController("/templates/controller.java");
        templateConfig.setEntity("/templates/entity.java");
        templateConfig.setMapper("/templates/mapper.java");
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //此处可以修改为您的表前缀
        strategy.setTablePrefix(new String[]{""});
        strategy.setTableFillList(new ArrayList<TableFill>());
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(tableNameList);
        // 排除生成的表
        //strategy.setExclude(new String[]{"test"});
//        strategy.setSuperControllerClass("com.dev.mybatisPlus.controller.BaseController");
        strategy.setEntityLombokModel(true);

        mpg.setStrategy(strategy);

        // 执行生成
        mpg.execute();
    }

}

